/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POS_ViewCart.java
 *
 * Created on Apr 4, 2011, 7:13:38 PM
 */
package DesktopGUIpackage;

import Cart.Cart;
import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Address;
import DTO.Item;
import DTO.ShippingMethod;
import Exception.KwickKioskDBException;
import Session.SessionManager;
import Utilities.MoneyFormatter;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author edaype
 */
public class Desktop_ViewCart extends javax.swing.JPanel {

    private Cart cart;
    private DesktopController desktopController = null;
    private DesktopControllerHelper desktopControllerHelper = null;
    private static final String[] columnNames = new String[]{"Name", "Quantity", "Price"};
    private MoneyFormatter moneyFormatter;
    private SessionManager sessionManager;
    private ArrayList<ShippingMethod> shippingMethods;
    private ArrayList<Address> addList;

    /** Creates new form POS_MainOut
     * @param control
     * @param cart 
     */
    public Desktop_ViewCart(DesktopControllerHelper desktopControllerHelper) {
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        initComponents();
        moneyFormatter = new MoneyFormatter();
        sessionManager = desktopController.getSessionMgr();
        cart = sessionManager.getCart();
        shippingMethods = new ArrayList<ShippingMethod>();
        addList = new ArrayList<Address>();
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        ButtonLayout = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        CheckoutButton = new javax.swing.JButton();
        ErrorLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        SubTotalAmount = new javax.swing.JLabel();
        TaxAmount = new javax.swing.JLabel();
        TotalAmount = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        DiscountAmount = new javax.swing.JLabel();
        ShippingCostAmount = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        DecreaseButton = new javax.swing.JButton();
        IncreaseButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        ShippingMethodBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        AddressBox = new javax.swing.JComboBox();
        EstimatedTimeLabel = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        jLabel1.setText("Cart Review");

        CheckoutButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        CheckoutButton.setText("Checkout");
        CheckoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckoutButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ErrorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(CheckoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(CheckoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setText("Subtotal");

        jLabel3.setText("Tax");

        jLabel4.setText("________________________________");

        jLabel5.setText("Total");

        SubTotalAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        TaxAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        TotalAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        jLabel6.setText("Total Discount");

        DiscountAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        ShippingCostAmount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        jLabel7.setText("Shipping Cost");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(14, 14, 14)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(TaxAmount, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(SubTotalAmount, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(DiscountAmount, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                                    .addComponent(TotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ShippingCostAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(ShippingCostAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(DiscountAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(SubTotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(TaxAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(TotalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        DecreaseButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DecreaseButton.setText("-");
        DecreaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DecreaseButtonActionPerformed(evt);
            }
        });

        IncreaseButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        IncreaseButton.setText("+");
        IncreaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IncreaseButtonActionPerformed(evt);
            }
        });

        DeleteButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "Make", "Model", "Price", "Quantity"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(DecreaseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(IncreaseButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(DeleteButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(IncreaseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DecreaseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DeleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        ShippingMethodBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShippingMethodBoxActionPerformed(evt);
            }
        });

        jButton1.setText("Add Address");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel9.setText("Select Shipping Address:");

        jLabel8.setText("Choose Shipping Method:");

        AddressBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddressBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AddressBox, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(ShippingMethodBox, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AddressBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ShippingMethodBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(EstimatedTimeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 178, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EstimatedTimeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void IncreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IncreaseButtonActionPerformed
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.increaseQuantity(index);
                } catch (KwickKioskDBException ex) {
                    desktopControllerHelper.DatabaseDownScreen();
                }
                Item item = cart.getItemsInCart().get(index);
                jTable1.setValueAt(item.getQty(), selected, 1);
                jTable1.setValueAt(moneyFormatter.format(item.getSoldPrice() * item.getQty()), selected, 2);
                if (item.getDiscountAmount() > 0.0 && sessionManager.getLoggedIn()) {
                    jTable1.setValueAt(item.getQty(), selected + 1, 1);
                    jTable1.setValueAt("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()), selected + 1, 2);
                }
                setTotals();
                jTable1.repaint();
                desktopControllerHelper.refresh();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }
    }//GEN-LAST:event_IncreaseButtonActionPerformed

    private void DecreaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DecreaseButtonActionPerformed
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.decreaseQuantity(index);
                } catch (KwickKioskDBException ex) {
                    desktopControllerHelper.DatabaseDownScreen();
                }
                Item item = cart.getItemsInCart().get(index);
                jTable1.setValueAt(item.getQty(), selected, 1);
                jTable1.setValueAt(moneyFormatter.format(item.getSoldPrice() * item.getQty()), selected, 2);
                if (item.getDiscountAmount() > 0.0 && sessionManager.getLoggedIn()) {
                    jTable1.setValueAt(item.getQty(), selected + 1, 1);
                    jTable1.setValueAt("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()), selected + 1, 2);
                }
                setTotals();
                jTable1.repaint();
                desktopControllerHelper.refresh();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }
    }//GEN-LAST:event_DecreaseButtonActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        int selected = jTable1.getSelectedRow();
        if (selected != -1) {
            String selectedName = jTable1.getValueAt(selected, 0).toString();
            if (!selectedName.equals("Discount")) {
                int index = cart.find(selectedName);
                try {
                    cart.removeItem(index);
                } catch (KwickKioskDBException ex) {
                    desktopControllerHelper.DatabaseDownScreen();
                }
                desktopControllerHelper.cartScreen();
            }
        } else {
            ErrorLabel.setText("No Item Selected.");
        }

    }//GEN-LAST:event_DeleteButtonActionPerformed

    private void CheckoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckoutButtonActionPerformed
        if (!sessionManager.getCart().getItemsInCart().isEmpty()) {
            if (sessionManager.getLoggedIn()) {
                if (shippingMethods != null) {
                    ShippingMethod selectedShipMethod = (ShippingMethod) ShippingMethodBox.getModel().getSelectedItem();
                    if (selectedShipMethod != null) {
                        Address selectedShipAddress = (Address) AddressBox.getModel().getSelectedItem();
                        if (selectedShipAddress != null) {
                            try {
                                sessionManager.setShipMethodForOrder(selectedShipMethod);
                            } catch (KwickKioskDBException ex) {
                                desktopControllerHelper.DatabaseDownScreen();
                            }
                            sessionManager.setShipaddressForOrder(selectedShipAddress);
                            desktopControllerHelper.paymentScreen();
                        } else {
                            ErrorLabel.setText("No shipping address selected.");
                        }
                    } else {
                        ErrorLabel.setText("No shipping method selected.");
                    }
                } else {
                    ErrorLabel.setText("No shipping address selected.");
                }

            } else {
                desktopController.getSessionMgr().setOnPathToPurchase(true);
                desktopControllerHelper.LogInScreen();
            }
        } else {
            ErrorLabel.setText("No Items in Cart.");
        }
    }//GEN-LAST:event_CheckoutButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (sessionManager.getLoggedIn()) {
            desktopController.getSessionMgr().setOnPathToPurchase(true);
            desktopControllerHelper.addAddressScreen();
        } else {
            desktopController.getSessionMgr().setOnPathToPurchase(true);
            desktopControllerHelper.LogInScreen();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void AddressBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddressBoxActionPerformed
        setShippingMethods();
        if (ShippingMethodBox.getModel().getSelectedItem() != null) {
            try {
                sessionManager.setShipMethodForOrder((ShippingMethod) ShippingMethodBox.getModel().getSelectedItem());
            } catch (KwickKioskDBException ex) {
                desktopControllerHelper.DatabaseDownScreen();
            }
        }
        setTotals();
    }//GEN-LAST:event_AddressBoxActionPerformed

    private void ShippingMethodBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ShippingMethodBoxActionPerformed
        if (ShippingMethodBox.getModel().getSelectedItem() != null) {
            try {
                ShippingMethod method = (ShippingMethod) ShippingMethodBox.getModel().getSelectedItem();
                sessionManager.setShipMethodForOrder(method);
                EstimatedTimeLabel.setText("Estimated delivery time: " + method.getDuration() + " day(s).");
            } catch (KwickKioskDBException ex) {
                desktopControllerHelper.DatabaseDownScreen();
            }
        }
        setTotals();
    }//GEN-LAST:event_ShippingMethodBoxActionPerformed

    public final void customInit() {
        if (sessionManager.getLoggedIn()) {
            setAddresses();
        }
        initTable();
        setTotals();

    }

    private void setShippingMethods() {
        shippingMethods = null;
        ShippingMethodBox.removeAllItems();
        Address add = (Address) AddressBox.getModel().getSelectedItem();
        if (add != null) {
            String pv = add.getProvince();

            shippingMethods = desktopController.getShippingMethods(pv);

        }
        ShippingMethodBox.addItem(null);
        for (ShippingMethod method : shippingMethods) {

            ShippingMethodBox.addItem(method);
        }
    }

    private void setAddresses() {
        int custId = sessionManager.getCutomerId();

        addList = (ArrayList<Address>) desktopController.getCustomerAddresses(custId);

        if (addList != null) {
            for (Address addr : addList) {
                AddressBox.addItem(addr);
            }
        }

    }

    private void initTable() {
        ListIterator<Item> iter = cart.getItemsInCart().listIterator();
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        while (iter.hasNext()) {
            Item item = iter.next();
            Vector row = new Vector();
            row.add(item.getName());
            row.add(item.getQty());
            row.add(moneyFormatter.format(item.getSoldPrice() * item.getQty()));
            model.addRow(row);
            if (item.getDiscountAmount() > 0.0 && sessionManager.getLoggedIn()) {
                row = new Vector();
                row.add("Discount");
                row.add(item.getQty());
                row.add("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()));
                model.addRow(row);
            }
        }

        jTable1.setModel(model);

        TableColumn column = null;
        column = jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(375); //third column is bigger
        column = jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(25);
        column = jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(50);

        jTable1.setRowHeight(40);

        jTable1.setColumnSelectionAllowed(false);
        jTable1.setRowSelectionAllowed(true);
    }

    private void setTotals() {
        ShippingCostAmount.setText(moneyFormatter.format(cart.getShippingCost()));
        DiscountAmount.setText(moneyFormatter.format(cart.getTotalDiscount()));
        SubTotalAmount.setText(moneyFormatter.format(cart.getSubtotal()));
        TaxAmount.setText(moneyFormatter.format(cart.getTaxAmount()));
        TotalAmount.setText(moneyFormatter.format(cart.getTotal()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AddressBox;
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JButton CheckoutButton;
    private javax.swing.JButton DecreaseButton;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JLabel DiscountAmount;
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JLabel EstimatedTimeLabel;
    private javax.swing.JButton IncreaseButton;
    private javax.swing.JLabel ShippingCostAmount;
    private javax.swing.JComboBox ShippingMethodBox;
    private javax.swing.JLabel SubTotalAmount;
    private javax.swing.JLabel TaxAmount;
    private javax.swing.JLabel TotalAmount;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
